<template>
  <el-dialog
    :title="'职务管理'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-table :data="jobList" style="width: 100%">
      <el-table-column prop="jobName" label="职务名称" width="180">
      </el-table-column>
      <el-table-column prop="sortNumber" label="排序" width="180">
      </el-table-column>
      <el-table-column prop="sortNumber" label="操作" width="180">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="openJob(scope.row.jobId)"
            >修改</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="deleteHandle(scope.row.jobId)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="innerVisible = true;jobId=''"
        >添加职务</el-button
      >
      <el-button @click="visible = false">取消</el-button>
    </span>
    <el-dialog
      width="30%"
      title="新增职务"
      :visible.sync="innerVisible"
      append-to-body
    >
      <div class="conts">
        <el-input v-model="jobName" placeholder="请输入职务名称"></el-input>
        <el-button  style="margin-left: 10px" type="primary" @click="addJob()"
          >保存</el-button
        >
      </div>
    </el-dialog>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      jobId:'',
      isdisabled:false,
      jobName: "",
      innerVisible: false,
      visible: false,
      jobList: [],
    };
  },
  methods: {
    openJob(jobId){
      this.jobId=jobId;
      this.innerVisible=true;
      this.jobList.forEach((item, index) => {
          if (item.jobId == this.jobId) {
            this.jobName =  item.jobName
          }
        });
    },
    getJobList() {
      this.visible = true;
      this.$http({
        url: this.$http.adornUrl(`/admin/user/jobList`),
        method: "get",
        params: this.$http.adornParams(),
      }).then(({ data }) => {
        if (data && data.code === "200") {
          this.jobList = data.list;
        }
      });
    },
    deleteHandle(jobId){
      this.jobId=jobId;
      var arr= this.jobList.filter(item =>item.jobId!=jobId);

      arr.forEach((item,index)=>{
        item.sortNumber=index+1
      })
      this.$http({
        url: this.$http.adornUrl(`/admin/user/batchUpdateJob`),
        method: "post",
        data: this.$http.adornData({
          sysJobList: arr,
        }),
      }).then(({ data }) => {
        if (data && data.code === "200") {
          this.getJobList();
          this.innerVisible = false;
        }
      });
    },
    addJob() {
      if (!this.jobName) {
        this.$message({
          message: "请输入职务名称",
          type: "error",
          duration: 1500,
        });
        return;
      }
      if (!this.jobId) {
        this.jobList.push({
            jobName: this.jobName,
            sortNumber:this.jobList.length+1
        })
      } else {
        this.jobList.forEach((item, index) => {
          if (item.jobId == this.jobId) {
            item.jobName = this.jobName;
          }
        });
      }
      this.isdisabled=true
      this.$http({
        url: this.$http.adornUrl(`/admin/user/batchUpdateJob`),
        method: "post",
        data: this.$http.adornData({
          sysJobList: this.jobList  ,
        }),
      }).then(({ data }) => {
        if (data && data.code === "200") {
          this.getJobList();
          this.innerVisible = false;
          this.isdisabled=false
        }
         this.isdisabled=false
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.conts {
  display: flex;
}
</style>
